"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 备设备pg_log目录无权限业务正常运行
Create At   : 2020/07/27
Owner       : @li-xin12345
Description :
    1. 启动TPCC业务
    2. 执行TPCC业务过程中备设备删除pg_log权限
    3. 查看TPCC业务是否被影响
Expect      :
    1. TPCC业务执行正常
    2. 删除pg_log权限成功
    3. TPCC业务执行正常
History     :
    modified by: @wan005 优化脚本，原脚本在用例失败情况下无法打印日志，导致用例失败无法定位，调整日志位置
"""
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread

logger = Logger()


class ModifyPgxlogPermi(unittest.TestCase):
    primary_sh = CommonSH('PrimaryDbUser')
    standby_1_sh = CommonSH('Standby1DbUser')
    standby_2_sh = CommonSH('Standby2DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case187.py start------------')
        self.rootNode = Node(node='PrimaryRoot')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.standbyRoot = Node(node='Standby1Root')
        self.com = Common('PrimaryDbUser')
        self.Constant = Constant()

    def test_delpermission_pgxlog(self):
        logger.info('---------------------------开始执行TPCC-------------------------')
        tpcc_thread = ComThread(self.com.startTPCC, args=(self.rootNode, macro.TPCC_PATH,))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()
        time.sleep(5)

        logger.info('---------------------删除pg_log权限---------------------------')
        modfiyPermisCmd = 'chmod -R 000 ' + macro.PG_LOG_PATH
        logger.info(modfiyPermisCmd)
        modifyMsg = self.standbyRoot.sh(modfiyPermisCmd).result()
        logger.info(modifyMsg)

        showPermissionCmd = 'ls -al  ' + macro.PG_LOG_PATH.rsplit("/", 1)[0]
        logger.info(showPermissionCmd)
        showMsg = self.rootNode.sh(showPermissionCmd).result()
        logger.info(showMsg)

        tpcc_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        tpcc_result = tpcc_thread.get_result()
        logger.info(tpcc_result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, tpcc_result)

        # 校验刷脏页是否完成
        logger.info('校验刷脏页是否完成')
        dirty_page_finshed_flag = self.primary_sh.check_whether_remain_dirty_page()
        self.assertTrue(dirty_page_finshed_flag)

        # 校验备机是否完成数据同步
        logger.info('校验备机是否完成数据同步')
        flag_1 = self.standby_1_sh.check_data_consistency()
        self.assertTrue(flag_1)
        flag_2 = self.standby_2_sh.check_data_consistency()
        self.assertTrue(flag_2)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        logger.info('---------------------------恢复pg_log权限-------------------------')
        modfiyPermisCmd = 'chmod -R 700 ' + macro.PG_LOG_PATH
        modifyMsg = self.standbyRoot.sh(modfiyPermisCmd).result()
        logger.info(modifyMsg)
        logger.info('----------------------------reliability_dbsys187执行完成-----------------------------')
